﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EDiamondModel;
using System.Data;
using System.IO;
using System.Text;

public partial class Pages_DangKy : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            double sl = GioHang.TongSanpham();
            double tg = GioHang.TongTrigia();
            Label SL = this.Page.Master.FindControl("SLuong") as Label;
            if (SL != null)
                SL.Text = "<b>" + sl.ToString() + "<b>";
            Label TGia = this.Page.Master.FindControl("TGia") as Label;
            if (TGia != null)
                TGia.Text = "<b>" + tg.ToString() + "<b>";
        }
    }

    EDiamondEntities1 db = new EDiamondEntities1();
    protected void btnDangKy_Click(object sender, EventArgs e)
    {
        try
        {
            if (!KiemtratenDangNhap(txtTenDangNhap.Text.Trim()))
                return;
            {
                kiemTra(txtTenDangNhap.Text);
                KhachHang entity = db.KhachHangs.CreateObject();
                entity.TenDangNhap = txtTenDangNhap.Text;
                entity.MatKhau = txtMatKhau.Text;
                entity.HoTen = txtHoTen.Text;
                entity.DiaChi = txtDiaChi.Text;
                entity.DienThoai = txtDienThoai.Text;
                entity.Email = txtEmail.Text;
                entity.NgaySinh = DateTime.Parse(txtNgaySinh.Text);
                entity.GioiTinh = CBox.Checked;

                if (FileUpload1.HasFile) entity.Hinh = entity.TenDangNhap + Path.GetExtension(FileUpload1.FileName);

                db.AddToKhachHangs(entity);
                db.SaveChanges();
                if (FileUpload1.HasFile)
                {
                    string f = "~/Photos/KhachHang/" + entity.Hinh;
                    FileUpload1.SaveAs(Server.MapPath(f));
                    Image3.ImageUrl = f;
                }
                lbKetQua.Text = string.Format("<h3>Xin chào  {0}</3>", entity.HoTen);
                MultiView1.SetActiveView(View2);
            }
        }
        catch (Exception ex)
        {
            lbKetQua.Text = ex.Message;
        }
    }
    private void kiemTra(string tendn)
    {
        StringBuilder err = new StringBuilder();
        if (string.IsNullOrEmpty(tendn))
        {
            err.AppendLine("Tên đăng nhập không được trống");

        }
        else
        {
            int d = db.KhachHangs.Count(p => p.TenDangNhap == tendn);
            if (d > 0)
            {
                throw new Exception("Tên đăng nhập này đã có người đăng ký!");
            }

        }
        // Những kiểm tra khác nếu có
        if (!string.IsNullOrEmpty(err.ToString()))
        {
            throw new Exception(err.ToString());
        }
    }
    private bool KiemtratenDangNhap(string tendn)
    {
        int d = db.KhachHangs.Count(p => p.TenDangNhap == tendn);
        if (d > 0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (!KiemtratenDangNhap(txtTenDangNhap.Text.Trim()))
        {
            args.IsValid = false;

        }
        else
            args.IsValid = true;
    }
}